﻿// PDFWebViewer.NET client script interaction demo
//
// This is a very simple demonstration of how to setup a custom search
// dialog.

var resultElement;
var findButton;
var cancelButton;

function initializeControls() {
   // find elements and setup event handlers
   resultElement = $get("results");
   findButton = $get("buttonFind");
   cancelButton = $get("buttonCancel");
   var viewer = $find("pdfViewer");
    // Setup handlers for search status events
   viewer.add_searching(onSearch);
   viewer.add_searchCompleted(onSearchCompleted);
    // Setup handlers for button clicks
    $addHandlers(findButton, { click: onButtonSearchClicked });
    $addHandlers(cancelButton, { click: onButtonCancelClicked });
}

function onButtonSearchClicked(event) {
    event.preventDefault(); // prevent postback
    // start search
    $find('pdfViewer').search($get('TextFind').value, $get('checkMatchCase').checked, $get('checkMatchWord').checked);
}

function onButtonCancelClicked(event) {
    event.preventDefault(); // prevent postback
    // cancel search
    $find('pdfViewer').cancelSearch();
}

function onSearch(sender, args) {
    // Searching has started
    _updateUI(true, "Searching..." );
}

function onSearchCompleted(sender, args) {
    // Searching has ended
   _updateUI(false, args.get_isSearchComplited(), args.get_isSearchSucceeded());
}

function _updateUI(pending, isComplited, isSucceeded) {
    // enable / disable buttons
    cancelButton.disabled = pending ? '' : 'disabled';
    findButton.disabled = pending ? 'disabled' : '';

    var status = String.format('<em>{0}</em>', "No matches found");
    
    if (isComplited) {
      status = String.format('<em>{0}</em>', "Search complited");
    } else if (isSucceeded) {
      status = String.format('<em>{0}</em>', "Found");
    }

    resultElement.innerHTML = status;
}


if (typeof (Sys) !== "undefined") {
   Sys.Application.add_load(initializeControls);
   Sys.Application.notifyScriptLoaded();
}